package com.magic.himalaya.views;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.magic.himalaya.R;
import com.magic.himalaya.adapters.PlayerListAdapter;
import com.magic.himalaya.base.BaseApplication;
import com.magic.himalaya.presenters.PlayerPresenter;
import com.magic.himalaya.utils.Constants;
import com.magic.himalaya.utils.DisplayUtils;
import com.magic.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import org.w3c.dom.Text;

import java.util.List;

/**
 * @description:
 * @create: Create by Magic on 2022/1/24.
 */
public class PlayerListPopupWindow extends PopupWindow {
    private static final String TAG = PlayerListPopupWindow.class.getSimpleName();
    private final View mView;
    private TextView mBtn_close;
    private PlayerPresenter mPlayerPresenter;
    private RecyclerView mRecyclerView;
    private PlayerListAdapter mPlayerListAdapter;
    private View mBtn_asc_desc;
    private ImageView mImage_asc_desc;
    private TextView mText_asc_desc;
    private View mBtn_switch_mode;
    private ImageView mImage_switch_mode;
    private TextView mText_switch_mode;

    public PlayerListPopupWindow() {
        //设置宽高
        super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //点击popupWindow外屏幕将关闭popupWindow
        setOutsideTouchable(true);
        //获取内容
        mView = LayoutInflater.from(BaseApplication.getsAppContext()).inflate(R.layout.popupwindow_show_player_list, null, false);

        //设置内容
        setContentView(mView);
        //设置出现和关闭的动画效果
        setAnimationStyle(R.style.pop_animation);


        initView();
        initEvent();

    }


    private void initView() {
        mPlayerPresenter = PlayerPresenter.getInstance();//获得播放器实例

        mBtn_close = mView.findViewById(R.id.close_player_list);//关闭按钮
        mRecyclerView = mView.findViewById(R.id.player_list_recycler);//获得播放列表展示组件
        mBtn_asc_desc = mView.findViewById(R.id.btn_player_list_asc_desc);//包裹升降序组件的一块LinearLayout区域
        mImage_asc_desc = mView.findViewById(R.id.image_asc_desc);//升降序的图标
        mText_asc_desc = mView.findViewById(R.id.text_asc_desc); //升降序的文字
        mBtn_switch_mode = mView.findViewById(R.id.btn_player_list_switch_mode);//包裹切换播放模式的一块LinearLayout区域
        mImage_switch_mode = mView.findViewById(R.id.image_switchPlayMode);//切换播放模式的图标
        mText_switch_mode = mView.findViewById(R.id.text_switchPlayMode);//切换播放模式的文字


        //设置RecyclerView的LayoutManager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContentView().getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        //设置适配器
        mPlayerListAdapter = new PlayerListAdapter();
        mRecyclerView.setAdapter(mPlayerListAdapter);

        //初始化播放模式


    }

    private void initEvent() {
        mBtn_close.setOnClickListener(v -> dismiss());//设置列表关闭按钮
        //设置列表点击切歌
        mPlayerListAdapter.setOnClickCallBack((position) -> {
            mOnClickPlayerListItemListener.switchPlaying(position);
        });
        //更改播放模式
        mBtn_switch_mode.setOnClickListener(v -> {
            if (mOnClickPlayerListItemListener != null) {
                mOnClickPlayerListItemListener.switchPlayerMode();
            }
        });
    }


    //==========================   以下数据设置可能会有问题，以下方法涉及到设置适配器数据

    /**
     * 添加播放列表的数据
     *
     * @param trackList
     */
    public void addPlayerList(List<Track> trackList) {
        mPlayerListAdapter.setData(trackList);
    }

    /**
     * 设置播放位置
     *
     * @param currentIndex
     */
    public void setCurrentPlayerIndex(int currentIndex) {
        mPlayerListAdapter.setIndex(currentIndex);//设置当前播放的位置
        mRecyclerView.scrollToPosition(currentIndex);//显示到当前坐标的位置
    }

    /**
     * 设置当前播放模式的图标和文字
     * @param resId
     * @param str
     */
    public void setPlayerMode(@DrawableRes int resId, String str){
        mImage_switch_mode.setImageResource(resId);
        mText_switch_mode.setText(str);
    }

    // ========================== 以下接口回调
    public interface OnClickPlayerListItemListener {
        void switchPlaying(int position);
        void switchPlayerMode();
    }

    private OnClickPlayerListItemListener mOnClickPlayerListItemListener = null;

    public void setOnClickPlayerListItemListener(OnClickPlayerListItemListener onClickPlayerListItemLinstener) {
        mOnClickPlayerListItemListener = onClickPlayerListItemLinstener;
    }

    /////
}
